package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wx
 * @version 1.0
 * @description: TODO
 * @date 2024/11/6 10:47
 */
@RestController
@RequestMapping("/admin/system/index")
@Tag(name = "首页接口")
@Slf4j
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;


    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.loginOut(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @PostMapping("/login")
    @Operation(summary = "用户登录")
//    @Parameters(@Parameter(name = "loginDto", description = "用户登录数据"))
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);

        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/generateValidateCode")
    @Operation(summary = "获取图片验证码")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/userinfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
//        SysUser sysUser = sysUserService.getUserInfo(token);
        //直接从线程变量中获取用户信息
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }


}
